package com.mentaleye.controller;

import java.util.Date;
import java.util.List;

import com.mentaleye.biz.PromptBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.DormitoryStudentBean;
import com.mentaleye.bean.DormitorysBean;
import com.mentaleye.biz.DormitoryBiz;
import com.mentaleye.biz.DormitoryStudentBiz;
import com.mentaleye.enums.CommonStatus;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping(value = "/dormitory")
public class DormitoryController {

	@Autowired
	private DormitoryBiz dormitoryBiz;
	@Autowired
	private DormitoryStudentBiz dormitoryStudentBiz;
	@Autowired
	private PromptBiz promptBiz;

	//获得宿舍列表视图
	@RequestMapping("/dormitoryList")
	public ModelAndView list() {
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryList");
		view.addObject("enumMap", JSONObject.toJSONString(CommonStatus.enumMap));
		return view;
	}
	//获得宿舍列表数据
	@RequestMapping("/dormitoryListData")
	@ResponseBody
	public String dormitoryListData(CommonPage commonPage) {
		dormitoryBiz.queryDormitoryByPage(commonPage);
		 System.out.println(commonPage.toSHowPage());
		return commonPage.toSHowPage();
	}
	//宿舍编辑视图
	@RequestMapping("/dormitoryEdit")
	public ModelAndView dormitoryEdit(@RequestParam(value = "dorId", required = false) Integer dorId) {
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryEdit");
		DormitorysBean dormitoryBean = dormitoryBiz.queryDormitoryById(dorId);
		view.addObject("dormitory", dormitoryBean);
		view.addObject("dorId", dorId);
		return view;
	}
	//宿舍详情视图
	@RequestMapping("/dormitoryDetail")
	public ModelAndView dormitoryDetail(@RequestParam(value = "dorId", required = false) Integer dorId) {
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryDetail");
		List<DormitoryStudentBean> dormitoryList = dormitoryStudentBiz.queryDormitoryRelateStudentById(dorId);
		int count = dormitoryStudentBiz.queryStudentCountByDorId(dorId);
		if(count==0){
			view.addObject("usedBed", 0);
		}else{
			view.addObject("usedBed", count);
		}
		int restBed = dormitoryBiz.queryDormitoryById(dorId).getSumBed()-count;
		DormitorysBean dormitorysBean = new DormitorysBean();
		dormitorysBean.setDorId(dorId);
		dormitorysBean.setRestBed(restBed);
		dormitoryBiz.editDormitory(dormitorysBean);
		 view.addObject("dormitoryList",dormitoryList);
		DormitorysBean dormitoryBean = dormitoryBiz.queryDormitoryById(dorId);
		view.addObject("dormitory", dormitoryBean);
		return view;
	}

	@RequestMapping("/save")
	public @ResponseBody String save(DormitorysBean dormitorysBean) {
		if (dormitorysBean.getExpenseStatus()==7){
				promptBiz.promptSave("宿舍费用",new Date(),"1",null,CommonStatus.PAYMENT_REMINDER);
		}
		return JSONObject.toJSONString(dormitoryBiz.editDormitory(dormitorysBean));
	}

	@RequestMapping("/delete")
	public @ResponseBody String delete(@RequestBody List<Integer> ids) {
		return JSONObject.toJSONString(dormitoryBiz.removeDormitory(ids));
	}
	
	@RequestMapping("/queryStudent")
	public @ResponseBody String queryStudentByDormitory(@RequestBody List<Integer> ids) {
		return JSONObject.toJSONString(dormitoryBiz.queryStudentByDormitory(ids));
	}
}
